using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace RunAliasCreator
{
	public static class ExplorerContextMenu
	{
		public static bool CommandExists(string parentType, string commandName)
		{
			string parent = parentType;
			if (String.IsNullOrEmpty(parent)) parent = "*";
			parent = parent.Replace("\\", string.Empty);
			RegistryKey key = Registry.ClassesRoot.OpenSubKey(parent);
			RegistryKey subkey;
			if (key == null)
				throw new AccessViolationException("Access to the registry key was denied by the system.");
			string subkeyName = @"shell\" + commandName;
			subkey = key.OpenSubKey(subkeyName);
			bool exists = (subkey != null);
			if (exists) subkey.Close();
			key.Close();
			return exists;
		}

		public static bool CreateCommand(string parentType, string commandName, string commandPath)
		{ return CreateCommand(parentType, commandName, commandPath, true); }
		public static bool CreateCommand(string parentType, string commandName, string commandPath, bool overwrite)
		{
			string parent = parentType;
			if (String.IsNullOrEmpty(parent)) parent = "*";
			parent = parent.Replace("\\", string.Empty);
			RegistryKey key = Registry.ClassesRoot.OpenSubKey(parent, true);
			RegistryKey subkey, subsubkey;
			if (key == null)
				throw new AccessViolationException("Access to the registry key was denied by the system.");
			string subkeyName = @"shell\" + commandName;
			subkey = key.OpenSubKey(subkeyName, true);
			if (subkey != null && !overwrite)
			{
				subkey.Close();
				key.Close();
				return false;
			}
			if (subkey == null) subkey = key.CreateSubKey(subkeyName);
			if (subkey == null)
			{
				key.Close();
				throw new AccessViolationException("Access to the registry key was denied by the system.");
			}
			subkey.SetValue(string.Empty, commandName);
			subsubkey = subkey.OpenSubKey("command", true);
			if (subsubkey == null) subsubkey = subkey.CreateSubKey("command");
			if (subsubkey == null)
			{
				subkey.Close();
				key.Close();
				throw new AccessViolationException("Access to the registry key was denied by the system.");
			}
			subsubkey.SetValue(string.Empty, commandPath);
			subsubkey.Close();
			subkey.Close();
			key.Close();
			return true;
		}

		public static bool DeleteCommand(string parentType, string commandName)
		{
			string parent = parentType;
			if (String.IsNullOrEmpty(parent)) parent = "*";
			parent = parent.Replace("\\", string.Empty);
			RegistryKey key = Registry.ClassesRoot.OpenSubKey(parent, true);
			RegistryKey subkey;
			if (key == null)
				throw new AccessViolationException("Access to the registry key was denied by the system.");
			string subkeyName = @"shell\" + commandName;
			subkey = key.OpenSubKey(subkeyName, true);
			bool result = false;
			if (subkey != null)
			{
				subkey.Close();
				key.DeleteSubKeyTree(subkeyName);
				result = true;
			}
			key.Close();
			return result;
		}


	}

}
